﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ElingAppSystemCtrls
{
    public partial class EASNumberTextEdit : DevExpress.XtraEditors.TextEdit
    {
        public EASNumberTextEdit()
        {
            InitializeComponent();
            //this.ImeMode = System.Windows.Forms.ImeMode.Alpha;
            this.ImeMode = ImeMode.Disable;
            this.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            this.Properties.Appearance.Options.UseTextOptions = true;
            this.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            this.Properties.Mask.EditMask = "n2";
            this.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            this.Properties.Mask.ShowPlaceHolders = false;
            this.Properties.Mask.UseMaskAsDisplayFormat = true;
            this.Properties.NullValuePromptShowForEmptyValue = true;
            this.GotFocus += new EventHandler(EASNumberTextEdit_GotFocus);
            this.Spin += new DevExpress.XtraEditors.Controls.SpinEventHandler(EASNumberTextEdit_Spin);
            this.Click += new EventHandler(EASNumberTextEdit_Click);
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(EASNumberTextEdit_CustomDisplayText);
            
        }
        /// <summary>
        /// 自定义文本框显示值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EASNumberTextEdit_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (((EASNumberTextEdit)sender).Name == "txtFFreeArea")
            {
                if (e.Value != null)
                {
                    decimal value = 0;
                    if (decimal.TryParse(e.Value.ToString(), out value))
                    {
                        if (value == 0)
                        {
                            e.DisplayText = "";
                        }
                    }
                }
            }
        }

        ///// <summary>
        ///// 控件值重写
        ///// </summary>
        //public override object EditValue
        //{
        //    get
        //    {
        //        return base.EditValue;
        //    }
        //    set
        //    {
        //        //if (!IsZeroShow)
        //        //{
        //        //    if (value != null)
        //        //    {
        //        //        if (value.ToString() == "0")
        //        //        {
        //        //            value = DBNull.Value;
        //        //        }
        //        //    }
        //        //}
        //        base.EditValue = value;
        //    }
        //}
      
        /// <summary>
        /// 键盘按下事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (this.Properties.MaxLength == 1)
            {
                if ((e.KeyChar >= '0') && (e.KeyChar <= '9'))
                {
                    SendKeys.Send("{TAB}");
                }
            }
            base.OnKeyPress(e);
        }
       

        /// <summary>
        /// 控件点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EASNumberTextEdit_Click(object sender, EventArgs e)
        {
            if (this.Properties.MaxLength == 1 || this.Properties.MaxLength==2)
            {
                this.SelectAll();
            }
        }
        /// <summary>
        /// 防止滚轮影响
        /// <creator>wangl </creator>
        /// <date>2012-08-02</date>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EASNumberTextEdit_Spin(object sender, DevExpress.XtraEditors.Controls.SpinEventArgs e)
        {
            e.Handled = true;
        }
        /// <summary>
        /// 控件获取焦点事件
        /// <creator>wangl </creator>
        /// <date>2012-08-02</date>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EASNumberTextEdit_GotFocus(object sender, EventArgs e)
        {
            this.SelectionStart = 0;
            if (this.Properties.MaxLength == 1 || this.Properties.MaxLength == 2)
            {
                this.SelectAll();
            }
        }
        #region 属性
        [Category(CategoryName.Properties),Description("(目前已没有使用)用户控件自定义，设置控件值为0时是否显示"),DefaultValue(false)]
        public bool IsZeroShow
        {
            set;
            get;
        }
        #endregion

    }
}
